package cn.tedu.system.shippingcart.controller;


import cn.tedu.system.base.response.JsonResult;
import cn.tedu.system.shippingcart.pojo.dto.CartAddDto;
import cn.tedu.system.shippingcart.pojo.dto.CartUpdateDto;
import cn.tedu.system.shippingcart.pojo.vo.CartVO;
import cn.tedu.system.shippingcart.service.CartService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@Api(tags = "05.购物车模块")
@RestController
@RequestMapping("/api/v1/cart/")
public class CartController {
    @Autowired
    private CartService cartService;

    @ApiOperation("购物车添加功能")
    @ApiOperationSupport(order = 10)
    @PostMapping("add")
    public JsonResult addCart(@RequestBody CartAddDto cartAddDto, @ApiIgnore HttpSession session) {
        log.debug("购物车添加参数:{}", cartAddDto);
        cartService.addCart(cartAddDto,session);
        return JsonResult.ok();
    }

    @ApiOperation("购物车删除功能")
    @ApiOperationSupport(order = 20)
    @PostMapping("delete/{productId}")
    public JsonResult deleteCart(@PathVariable("productId") Long productId, @ApiIgnore HttpSession session){
        log.debug("购物车删除参数:{}", productId);
        cartService.deleteCart(productId,session);
        return JsonResult.ok();
    }
    @ApiOperation("购物车修改功能")
    @ApiOperationSupport(order = 30)
    @PostMapping("update")
    public JsonResult updateCart(@RequestBody CartUpdateDto cartUpdateDto, @ApiIgnore HttpSession session){
        log.debug("购物车修改参数:{}", cartUpdateDto);
        cartService.updateCart(cartUpdateDto,session);
        return JsonResult.ok();
    }

    @ApiOperation("购物车查询功能")
    @ApiOperationSupport(order = 40)
    @GetMapping("select")
    public JsonResult selectCart(@ApiIgnore HttpSession session){
        log.debug("购物车查询...");
        List<CartVO> list = cartService.selectCart(session);
        return JsonResult.ok(list);
    }

    @ApiOperation("购物车选中商品总价功能")
    @ApiOperationSupport(order = 50)
    @GetMapping("AllPrice")
    public JsonResult selectCartAllPrice(@ApiIgnore HttpSession session){
        log.debug("购物车查询选中商品总价...");
        Double allPrice = cartService.allPrice(session);
        return JsonResult.ok(allPrice);
    }
}



